/**
*
* @file
*		heap_vasya
*
* @brief
*		Heap implementation by anyav
*/

#ifndef __heap_vasya_H__
#define __heap_vasya_H__
//
#include <cstdlib>

#include <Windows.h>
#include <vector>
#include "../../api/heap.h"
#pragma once
#include <map>
/**
* basic interface
*/
const int const_total=1024*1024*70;




class heap_vasya: public heap876
{

private:
	// current volume of distributed memory
	size_t alloc_sz;    ///the size allocated
	int n_blocks;  //number of blocks
	char *mem;       //we use mem to get memory
    char *begin;  //pointer to the beginning of memory
    char *temp_ptr;
    std::vector <bool> mem_map; ///boolean map-array of free place
    std::vector <int> length_map; ///array to know the size for each alloc
    int n_size;  //number of blocks needed
    int temp; //to calculate blocks
    int first; //where in array start
    int i,j; //for counting
    void *result;
public:

    CRITICAL_SECTION cs;
	heap_vasya( size_t total, alloc_flags flag) : heap876(total, flag, "vasya")
	{
            if (!InitializeCriticalSectionAndSpinCount(&cs,
        0x00000400) ) {
            //printf("Critical Section not initialised!");
//            return 0;
        }
	    total_limit = const_total;
		alloc_sz = 0;
		mem = new char[total];  ///allocated total_limit bytes
        begin = mem;
        n_blocks = int(total_limit/16);
        mem_map.resize(n_blocks);
        length_map.resize(n_blocks);
            for(i = 0; i<n_blocks;i++) { ///maybe remove to other place
                mem_map[i] = false;
                length_map[i] = 0;
            }

	};
    size_t total_limit_public;
	virtual ~heap_vasya();
	virtual void*	alloc( size_t sz, unsigned int tag );
	virtual void	free( void* ptr );
	virtual void	free_all( unsigned int tag ){};

	virtual size_t get_occupancy() const { return alloc_sz; };

};


#endif//__heap_vasya_H__
